<?php
namespace app\beautiful\controller\admin\mall;
use app\BaseController;
use app\common\logic\DataLogic;
use app\common\logic\SmsLogic;
use app\beautiful\logic\printer\PrinterLogic;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\order\GoodsOrderItemModel;
use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\model\setting\SettingSmsModel;

class Order extends BaseController{
    public function getGoodsOrderNum()
    {
        $order_num=GoodsOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0],
                ['status','in',[2,4]]
            ])->count() ?? 0;
        return $this->success(['order_num'=>$order_num]);
    }
    
    public function index()
    {
        $order_id = $this->request->param('order_id/d',0);
        $order_type=$this->request->param('order_type');
        $mobile=$this->request->param('mobile');
        $status=$this->request->param('status/d',0);
        $member_id=$this->request->param('member_id/d',0);

        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];

        if($member_id == 0){
            $member_where=[
                ['shop_id','=',$this->request->shopId]
            ];
            if(!empty($mobile)){
                $member_where[]=['mobile','=',$mobile];
            }
            $member_ids= MemberModel::where($member_where)->column('member_id') ?? [];
            $where[]=['member_id','in',$member_ids];
        }else{
            $where[]=['member_id','=',$member_id];
        }



        if(!empty($order_id)) $where[]=['order_id','=',$order_id];
        if(!empty($order_type)) $where[]=['order_type','=',$order_type];
        if(!empty($status)) $where[]=['status','=',$status];

        $order_model=new GoodsOrderModel();
        $order_model=$order_model->with(['skus','member']);

        $order=['add_time'=>'desc'];
        $field="*";
        $append=['add_time_format','pay_time_format','status_means','order_type_means','pay_type_mean'];

        $return = DataLogic::getDataList($order_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        $deliver_num=GoodsOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['logistics_type','=',1],
            ['status','=',2]
        ])->count() ?? 0;

        $write_off_num=GoodsOrderModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['logistics_type','=',2],
            ['status','=',4]
        ])->count() ?? 0;

        $return['deliver_num']=$deliver_num;
        $return['write_off_num']=$write_off_num;
        return $this->success($return);

    }

    public function detail()
    {
        $order_id = $this->request->param('order_id/d',0);
        if($order_id==0) return $this->error('订单不存在#1');
        if(null === $order = GoodsOrderModel::find($order_id)) return $this->error('订单不存在#2');
        if($order->shop_id != $this->request->shopId) return $this->error('订单不存在#3');
        if($order->is_delete != 0) return $this->error('订单不存在#4');

        if(null === $member = MemberModel::find($order->member_id)) return $this->error('订单不存在#5');
        if($member->shop_id != $this->request->shopId) return $this->error('订单不存在#6');

        $items=GoodsOrderItemModel::where([
            ['shop_id','=',$this->request->shopId],
            ['order_id','=',$order_id]
        ])->select()->toArray();
        if(empty($items)) return $this->error('订单不存在#7');

        $order->append(['add_time_format','pay_time_format','status_means','order_type_means','pay_type_mean']);

        $detail=[
            'order_id'=>$order->order_id,
            'logistics_type'=>$order->logistics_type,
            'add_time_format'=>$order->add_time_format,
            'order_type_means'=>$order->order_type_means,
            'status'=>$order->status,
            'status_means'=>$order->status_means,
            'need_pay'=>$order->need_pay,
            'pay_type'=>$order->pay_type,
            'pay_type_mean'=>$order->pay_type_mean,
            'pay_time_format'=>$order->pay_time_format,
            'receiving_name'=>$order->receiving_name,
            'receiving_mobile'=>$order->receiving_mobile,
            'receiving_address_province'=>$order->receiving_address_province,
            'receiving_address_city'=>$order->receiving_address_city,
            'receiving_address_county'=>$order->receiving_address_county,
            'receiving_address_info'=>$order->receiving_address_info,
            'total_price'=>$order->total_price,
            'youhui_balance'=>$order->youhui_balance,
            'freight'=>$order->freight,
            'coupon_money'=>$order->coupon_money,
            'integral_balance'=>$order->integral_balance,
            'member_name'=>$member->nick_name,
            'member_mobile'=>$member->mobile,
            'items'=>$items
        ];

        return $this->success(['detail'=>$detail]);
    }
    
    public function overGoods(){
        $order_id = $this->request->param('order_id/d',0);
        if($order_id==0) return $this->error('订单不存在#1');
        if(null === $order = GoodsOrderModel::find($order_id)) return $this->error('订单不存在#2');
        if($order->shop_id != $this->request->shopId) return $this->error('订单不存在#3');
        if($order->is_delete != 0) return $this->error('订单不存在#4');
        if($order->logistics_type != 1) $this->error('该订单无法完成');
        if($order->status !=3) return $this->error('该订单无法完成');
        $order->status=8;
        $order->save();
        return $this->success('订单已完成');
    }

    public function deliverGoods()
    {
        $order_id = $this->request->param('order_id/d',0);
        if($order_id==0) return $this->error('订单不存在#1');
        if(null === $order = GoodsOrderModel::find($order_id)) return $this->error('订单不存在#2');
        if($order->shop_id != $this->request->shopId) return $this->error('订单不存在#3');
        if($order->is_delete != 0) return $this->error('订单不存在#4');
        if($order->logistics_type != 1) $this->error('该订单无法发货');
        if($order->status !=2) return $this->error('该订单无法发货');

        if(null === $member = MemberModel::find($order->member_id)) return $this->error('订单不存在#5');

        $order->status=3;
        $order->save();

        //其他要补充的
        //发送短信给用户
        try{
            $sms_setting = SettingSmsModel::where([['shop_id','=',$this->request->shopId]])->find();
            if(null !== $sms_setting){

                if($sms_setting->order_deliver_notice_member==1){
                    $sms_logic= new SmsLogic();
                    $params=[
                        'id'=>(string)$order_id,
                    ];
                    $sms_logic->send($this->request->shopId,[$member->mobile],$params,'orderDeliverNoticeMember');
                }
            }
        }catch (\Exception $e){

        }

        return $this->success('发货成功');
    }
    
    public function writeOff()
    {
        $order_id = $this->request->param('order_id/d',0);
        if($order_id==0) return $this->error('订单不存在#1');
        if(null === $order = GoodsOrderModel::find($order_id)) return $this->error('订单不存在#2');
        if($order->shop_id != $this->request->shopId) return $this->error('订单不存在#3');
        if($order->is_delete != 0) return $this->error('订单不存在#4');
        if($order->logistics_type != 2) $this->error('该订单无法核销');
        if($order->status !=4) return $this->error('该订单无法核销');

        if(null === $member = MemberModel::find($order->member_id)) return $this->error('订单不存在#5');

        $order->status=8;
        $order->save();
        
        //其他要补充的
        //发送短信给用户
        try{
            $sms_setting = SettingSmsModel::where([['shop_id','=',$this->request->shopId]])->find();
            if(null !== $sms_setting){

                if($sms_setting->order_write_off_notice_member==1){
                    $sms_logic= new SmsLogic();
                    $params=[
                        'id'=>(string)$order_id,
                    ];
                    $sms_logic->send($this->request->shopId,[$member->mobile],$params,'orderWriteOffNoticeMember');
                }
            }
        }catch (\Exception $e){

        }

        //打印机
        $printer_logic =new PrinterLogic($this->request->shopId);
        $printer_logic->print('orderWriteOff',$order->order_id);
        
        return $this->success('核销成功');
    }
}
